import api from '@/utils/requestUtil/api'
import { SHA1 } from '@/utils/SHA'
const defaultPageSize = 16 // 默认分页每页条数
const compareSuperUser = (pass) => {
  return (pass === SHA1(`1${sessionStorage.getItem('token')}`))
}
const user = {
  state: {
    id: sessionStorage.getItem('UserID') || '',
    roles: [],
    UserName: sessionStorage.getItem('UserName') || '',
    ShopAuthoritys: [],
    token: sessionStorage.getItem('token') || '',
    IsSuperUser: compareSuperUser(sessionStorage.getItem('Is_S_U')) || false,
    currentShopName: sessionStorage.getItem('currentShopName') || '',
    currentShopID: sessionStorage.getItem('currentShopID') || null,
    shopRegisterSwitch: (sessionStorage.getItem('shopRegisterSwitch') === 'opend') || false,
    shopCourseSwitch: (sessionStorage.getItem('shopCourseSwitch') === 'opend') || false,
    pageSize: parseInt(sessionStorage.getItem('GlobalPageSize')) || defaultPageSize,
    pageSizeList: [defaultPageSize, defaultPageSize * 2, defaultPageSize * 3, defaultPageSize * 4],
    adminNoticeSet: {} // 待处理事项通知消息
  },
  mutations: {
    SET_USER_NAME: (state, UserName) => {
      state.UserName = UserName
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_IS_SUPER_USER: (state, IsSuperUser) => {
      state.IsSuperUser = IsSuperUser
    },
    SET_USER_ID: (state, id) => {
      state.id = id
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_SHOP_AUTHORITYS: (state, ShopAuthoritys) => {
      state.ShopAuthoritys = ShopAuthoritys
    },
    SET_CURRENT_SHOP_NAME: (state, shopName) => {
      sessionStorage.setItem('currentShopName', shopName)
      state.currentShopName = shopName
    },
    SET_CURRENT_SHOP_ID: (state, shopID) => {
      sessionStorage.setItem('currentShopID', shopID)
      state.currentShopID = shopID
    },
    SET_SHOP_REGISTER_SWITCH: (state, switchState) => {
      sessionStorage.setItem('shopRegisterSwitch', switchState ? 'opend' : 'closed')
      state.shopRegisterSwitch = switchState
    },
    SET_SHOP_COURSE_SWITCH: (state, switchState) => {
      sessionStorage.setItem('shopCourseSwitch', switchState ? 'opend' : 'closed')
      state.shopCourseSwitch = switchState
    },
    SET_PAGE_SIZE: (state, pageSize) => {
      state.pageSize = pageSize
    },
    SET_ADMIN_NOTICE_SET: (state, adminNoticeSet) => {
      state.adminNoticeSet = adminNoticeSet
    }
  },
  actions: {
    pageSizeChange ({commit, state}, pageSize) { // 全局pageSize改变
      return new Promise(resolve => {
        sessionStorage.setItem('GlobalPageSize', pageSize)
        commit('SET_PAGE_SIZE', pageSize)
        resolve()
      })
    },
    // 管理员登录
    async AdminLogin ({commit, state}, {username, password}) {
      const result = await api.post(`/Login/Login`, {
        UserName: username,
        Password: password
      })
      if (result.Code === 100000) {
        let user = result.Data
        sessionStorage.setItem('UserID', user.AdminID)
        sessionStorage.setItem('UserName', user.AdminName)
        sessionStorage.setItem('token', user.Token)
        sessionStorage.setItem('Is_S_U', SHA1(`${user.IsSuperUser}${user.Token}`))
        commit('SET_TOKEN', user.Token)
        commit('SET_USER_ID', user.ID)
        commit('SET_USER_NAME', user.AdminName)
        commit('SET_IS_SUPER_USER', compareSuperUser(SHA1(`${user.IsSuperUser}${user.Token}`)))
        return {
          state: true
        }
      } else {
        return {
          state: false,
          msg: result.Msg
        }
      }
    },
    /**
     * 根据商户获取权限
     * @param {*} param
     */
    async GetShopAuthoritys ({commit, state}, {shopID}) {
      const result = await api.get(`/Login/GetShopAuthoritys`, {
        shopId: shopID
      })
      if (result.Code === 100000 && result.Data.length > 0) {
        commit('SET_SHOP_AUTHORITYS', result.Data)
        return {
          state: true
        }
      } else {
        commit('SET_SHOP_AUTHORITYS', null)
        return {
          state: false,
          msg: result.Msg
        }
      }
    },
    /**
     * 获取待处理的事项
     */
    async GetAdminNotice ({commit, state}) {
      const result = await api.get(`/Admin/GetNotice`)
      if (result.Code === 100000) {
        commit('SET_ADMIN_NOTICE_SET', result.Data)
      } else {
        this.$message.error(result.Msg)
      }
    },
    // 离开当前商户
    QuitOrganizationAction ({ commit, state }) {
      return new Promise(resolve => {
        commit('SET_CURRENT_SHOP_NAME', '')
        commit('SET_CURRENT_SHOP_ID', null)
        commit('SET_SHOP_AUTHORITYS', null)
        commit('SET_SHOP_REGISTER_SWITCH', null)
        commit('SET_SHOP_COURSE_SWITCH', null)
        commit('SET_ADMIN_NOTICE_SET', {})
        sessionStorage.removeItem('currentShopName')
        sessionStorage.removeItem('currentShopID')
        sessionStorage.removeItem('shopRegisterSwitch')
        sessionStorage.removeItem('shopCourseSwitch')
        resolve()
      })
    },
    /**
     * 管理员退出登录
     * @param {*} param
     */
    AdminLogoutAction ({commit, state}) {
      return new Promise(resolve => {
        user.actions.QuitOrganizationAction({commit, state})
        sessionStorage.removeItem('token')
        sessionStorage.removeItem('UserName')
        sessionStorage.removeItem('Is_S_U')
        sessionStorage.removeItem('UserID')
        commit('SET_TOKEN', '')
        commit('SET_USER_ID', '')
        commit('SET_USER_NAME', '')
        commit('SET_IS_SUPER_USER', false)
        resolve()
      })
    }
  }
}
export default user
